﻿using EnglishAuction.BUS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EnglishAuction
{
    public partial class MyProduct : System.Web.UI.Page
    {
        private void fillData()
        {
            NguoiDung nguoiDung = (NguoiDung)Session["CurrentUser"];
            gvMyProduct.DataSource = SanPhamBUS.GetByUser(nguoiDung);
            gvMyProduct.DataBind();
            gvDangDauGia.DataSource = SanPhamBUS.GetListDangDauGia(nguoiDung.Id);
            gvDangDauGia.DataBind();
            gvDaDuyet.DataSource = SanPhamBUS.GetListDaDuyet(nguoiDung.Id);
            gvDaDuyet.DataBind();

            var lstDauGiaThanhCong = SanPhamBUS.GetListDaDauGiaThanhCong(nguoiDung.Id);
            var tmp = from n in lstDauGiaThanhCong
                      select new
                      {
                          SanPhamId = n.Id,
                          TenSanPham = n.TenSanPham,
                          NgayLenSan = n.NgayLenSan,
                          GiaSan = n.GiaSan,
                          GiaThang = NguoiThangCuocBUS.GetNguoiThangCuocBySanPhamId(n.Id).Price,
                          NguoiThang = NguoiThangCuocBUS.GetNguoiThangCuocBySanPhamId(n.Id).Username,
                          NguoiThangId = NguoiThangCuocBUS.GetNguoiThangCuocBySanPhamId(n.Id).UserId,
                          ThanhToan = NguoiThangCuocBUS.GetNguoiThangCuocBySanPhamId(n.Id).Payment,
                          HinhAnh = n.HinhAnh
                      };
            gvDauGiaThanhCong.DataSource = tmp;
            gvDauGiaThanhCong.DataBind();

            gvDauGiaThatBai.DataSource = SanPhamBUS.GetListDaDauGiaThatBai(nguoiDung.Id);
            gvDauGiaThatBai.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            NguoiDung nguoiDung = (NguoiDung)Session["CurrentUser"];
            if (nguoiDung == null)
            {
                Response.Redirect("Default.aspx");
                return;
            }
            else if (nguoiDung.LoaiNguoiDungId == 2)
            {
                Response.Redirect("Default.aspx");
                return;
            }

            if (!IsPostBack)
            {
                fillData();
            }
        }

        protected void gvMyProduct_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvMyProduct.PageIndex = e.NewPageIndex;
            fillData();
        }

        protected void gvMyProduct_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }
    }
}